<?php
/* @var $this yii\web\View */
use yii\helpers\Url;
use yii\helpers\Html;
use yii\widgets\LinkPager;
use app_supplier\assets\SuppAssets;
SuppAssets::addArrayCss($this, [
    '@web/css/purchase-index.css',
    '@web/css/pur-index.css',
    '@web/carselect/css/common.css',
    '@web/css/page.css'
]);
SuppAssets::addScript($this,  '@web/js/jsAddress.js');
SuppAssets::addScript($this,  '@web/carselect/carselect.js');
?>

<div class="box">
    <div class="heading">
        <h1><img src="<?=Url::to('@web/images/')?>category.png" alt=""><?=$this->context->pageTitle?></h1>
        <div class="buttons">
            <a onclick="javascript:purchase.showDiv(); " class="button" id="condition-name"><span>隐藏筛选条件</span></a>
            <a class="button " href="<?=Url::to(['main/index'])?>"><span>返回</span></a>
        </div>
    </div>
    <div class="content">
        <div class="dashboard-content" style="margin-bottom: 20px;padding: 5px;" id="dashboard-content">
            <form id="condition" action="<?=Url::to([$this->context->id . '/'. $this->context->action->id])?>" method="get">
            <!--div class="row-1">
                <div class="row-12">
                    <span class="tip">全&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;部：</span>
                </div>
                <div class="row-2">
                    <input type="radio" name="time" value="all">
                </div>
                <div class="clear-fox"></div>
            </div-->
            <div class="row-1">
                <div class="row-12">
                    <span class="tip">日&nbsp;&nbsp;期：</span>
                </div>
                <div class="row-2">
                    <?php if (isset($condition['time']) && $condition['time'] == 'all'):?>
                        <input type="radio" name="time" value="all" checked>&nbsp;&nbsp;所有时间
                    <?php else:?>
                        <input type="radio" name="time" value="all">&nbsp;&nbsp;所有时间
                    <?php endif;?>
                    <?php if (isset($condition['time']) && $condition['time'] == 'today'):?>
                        <input type="radio" name="time" class="time" value="today" checked>&nbsp;&nbsp;今日
                    <?php else:?>
                        <input type="radio" name="time" class="time" value="today">&nbsp;&nbsp;今日
                    <?php endif;?>

                    <?php if (isset($condition['time']) && $condition['time'] == 'mouth'):?>
                        <input type="radio" name="time" class="time" value="mouth" checked>&nbsp;&nbsp;本月
                    <?php else:?>
                        <input type="radio" name="time" class="time" value="mouth">&nbsp;&nbsp;本月
                    <?php endif;?>

                    <?php if (isset($condition['time']) && $condition['time'] == 'period'):?>
                        <input type="radio" name="time" class="time" value="period" checked>&nbsp;&nbsp;期间
                    <?php else:?>
                        <input type="radio" name="time" class="time" value="period">&nbsp;&nbsp;期间
                    <?php endif;?> 
                    
                    <input type="text" name="start" class="date" value="<?=$condition['start']?>">&nbsp;&nbsp;至
                    <input type="text" name="end" class="date" value="<?=$condition['end']?>">
                </div>
                <div class="clear-fox"></div>
            </div>
            <hr class="hr-dashed">
            <div class="row-1">
                <div class="row-12">
                    <span class="tip">区&nbsp;&nbsp;域：</span>
                </div>
                <div class="row-2">
                    <select name="cmbProvince" id="cmbProvince" style="width: 25%"></select>
                    <select name="cmbCity" id="cmbCity"  style="width: 25%"></select>
                    <select name="cmbArea" id="cmbArea"  style="width: 25%"></select>
                </div>
                <div class="clear-fox"></div>
            </div>
            <hr class="hr-dashed">
            <div class="row-1">
                <div class="row-12">
                    <span class="tip">材&nbsp;&nbsp;料：</span>
                </div>
                <div class="row-2">
                    <select name="material" id="material" style="width: 25%" onchange="purchase.changeBrand('brand')">
                        <option value="all">全部</option>
                        <?php if(isset($material) && is_array($material)):?>
                            <?php foreach ($material as $m):?>
                                <?php if (isset($condition['material']) && $condition['material'] == $m['id'] ):?>
                                    <option value="<?=$m['id']?>" selected><?=$m['name']?></option>
                                <?php else:?>
                                    <option value="<?=$m['id']?>"><?=$m['name']?></option>
                                <?php endif;?>
                            <?php endforeach;?>
                        <?php endif ?>
                    </select>
                    <select name="brand" id="brand"  style="width: 25%">
                        <option value="all">全部</option>
                        <?php if(isset($brand) && is_array($brand)):?>
                            <?php foreach ($brand as $b):?>
                                <?php if (isset($condition['brand']) && $condition['brand'] ==$b['brand'] ):?>
                                    <option value="<?=$b['brand']?>" selected><?=$b['brand']?></option>
                                <?php else:?>
                                    <option value="<?=$b['brand']?>"><?=$b['brand']?></option>
                                <?php endif;?>
                            <?php endforeach;?>
                        <?php endif ?>
                        <input type="hidden" value="<?=$condition['brand']?>" id="brand-condition">
                    </select>
                </div>
                <div class="clear-fox"></div>
            </div>
            <hr class="hr-dashed">
            <div class="row-1">
                <div class="row-12">
                    <span class="tip">车 &nbsp;&nbsp;型：</span>
                </div>
                <div class="row-2">
                    <input type="text" name="car_type" onclick="purchase.openCarType()" style="width: 28%;" value="<?=$condition['car_type']?>" placeholder="点击选择车型">
                </div>
                <div class="clear-fox"></div>
            </div>
            <hr class="hr-dashed">
            <div class="row-1">
                <div class="row-12">
                    <span class="tip"></span>
                </div>
                <div class="row-2" style="">
                    <a onclick="javascript:purchase.condition(); " class="button" style="margin-left: 25%;"><span>筛选条件</span></a>
                    <a onclick="javascript:purchase.cancelCondition()" class="button" style="margin-left: 15%;"><span>取消</span></a>
                </div>
                <div class="clear-fox"></div>
            </div>
                <input type="hidden" name="t" value="condition">
            </form>
        </div>
        <table id="tbl_list" class="list">
            <thead>
            <tr>
                <th class="center" width="1">
                    <input id="unchecked" onclick="$('input[name*=\'selected\']:not(:disabled)').attr('checked', this.checked);" type="checkbox">
                </th>
                <th class="center" width="60">
                    区&nbsp;&nbsp;域
                </th>
                <th class="center" width="120">
                    <?php if (isset($sort) && $sort=='create_time'):?>
                        <a href="<?=$sortUrl['create_time']?>" class="<?=$order?>">消耗时间</a>
                    <?php else:?>
                        <a href="<?=$sortUrl['create_time']?>">消耗时间</a>
                    <?php endif;?>
                </th>
                <th class="center">车型</th>
                <th class="center">配件类别</th>
                <th class="center">配件品牌</th>
                <th class="center">配件型号</th>
            </tr>
            </thead>
            <tbody>
            <?php if (isset($items) && !empty($items) && is_array($items)):?>

                <?php foreach ($items as $row):?>
                    <tr>
                        <td class="center">
                            <input name="selected[]" value="1" type="checkbox">
                        </td>
                        <td class="center"><?=$row['city_name']?></td>
                        <td class="center"><?=$row['create_time']?></td>
                        <td class="center"><?=$row['user_car']?></td>
                        <td class="center"><?=$row['material_name']?></td>
                        <td class="center"><?=$row['brand']?></td>
                        <td class="center"><?=$row['material_type']?></td>
                    </tr>
                <?php endforeach;?>
            <?php else:?>
                <tr>
                    <td class="center">
                        <input name="selected[]" value="1" type="checkbox">
                    </td>
                    <td class="center" colspan="6">
                        没有数据！
                    </td>
                </tr>
            <?php endif?>

            </tbody>
        </table>
        <?php if (isset($page) && !empty($page)):?>
            <?=LinkPager::widget([
                'pagination' => $page,
                'nextPageLabel' => '下一页',
                'prevPageLabel' => '上一页',
                'firstPageLabel' => '首页',
                'lastPageLabel' => '尾页',
            ])?>
        <?php endif;?>
    </div>
</div>
<script>
    var chebao_config = {
        storageDomain:"http://storage.51chebao.com/"
    } ;
    var purchase = {
        showDiv:function () {
            var obj = $("#dashboard-content");
            if (obj.is(":hidden")) {
                obj.show();
                $("#condition-name").html('隐藏筛选条件');
            } else {
                $("#condition-name").html('显示筛选条件');
                obj.hide();
            }
        },
        changeBrand: function (type) {
            var id = $("#material option:selected").val();
            $.get("<?=Url::to([$this->context->id . '/brand'])?>?id="+id, function (response) {
                if (response.status == 200 ) {
                    var option = '<option value="all">全部</option>';
                    var selected = $("#brand-condition").val();
                    //console.log(selected);
                    $.each(response.data, function (i, item) {
                        if (selected==item.name) {
                            option += "<option value='"+item.brand+"' selected>"+item.brand+"</option>"
                        } else {
                            option += "<option value='"+item.brand+"'>"+item.brand+"</option>"
                        }
                    });
                    $("#"+type).html(option);
                }
            })
        },
        openCarType:function () {
            selectcar.open("<?=Url::to([$this->context->id . '/car_type'])?>", function (data) {
                if (data.name) {
                    $("input[name='car_type']").attr('value', data.name);
                }
            })
        },
        condition:function () {
            $("#condition").submit();
        },
        cancelCondition:function () {
            var url = $('#condition').attr('action');
            var ref = 'time';
            location.href = this.delParams(url, ref)+"?t=all";
        },
        delParams:function (url, ref) {
            var str = "";
            if (url.indexOf('?') != -1) {
                str = url.substr(url.indexOf('?') + 1);
            }
            else {
                return url;
            }
            var arr = "";
            var return_url = "";
            if (str.indexOf('&') != -1) {
                arr = str.split('&');
                for (i in arr) {
                    if (arr[i].split('=')[0] != ref) {
                        return_url = return_url + arr[i].split('=')[0] + "=" + arr[i].split('=')[1] + "&";
                    }
                }
                return url.substr(0, url.indexOf('?')) + "?" + return_url.substr(0, return_url.length - 1);
            } else {
                arr = str.split('=');
                if (arr[0] == ref) {
                    return url.substr(0, url.indexOf('?'));
                }
                else {
                    return url+"?t=all";
                }
            }
        }
    };
    $(function () {
        addressInit('cmbProvince', 'cmbCity', 'cmbArea',"<?=$condition['cmbProvince']?>" , '<?=$condition['cmbCity']?>', '<?=$condition['cmbArea']?>');

        $('.date').datepicker({
            onSelect: function(dateText) {
                stat('distributor_during');
            }
        });

        function stat(stat_type)
        {
            if (stat_type == 'distributor_during') {
                var date_start = $("#stat_start").val();
                var date_end = $("#stat_end").val();
                if (!date_start || !date_start) {
                    return ;
                }
                start = new Date(date_start);
                end = new Date(date_end);

                if (Date.parse(end) < Date.parse(start)) {
                    layer.msg('结束时间不能早于开始时间！');
                    return;
                } else {
                    layer.msg('时间');
                }

//                url += '&stat_start=' + date_start;
//                url += '&stat_end=' + date_end;
//                console.log(url);
            }
            //location = url;
        }
    })
</script>
